import org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor as Events
import org.codehaus.groovy.grails.validation.MatchesConstraint

class FlexScaffoldController {

    def index = { }

	// get list of domain objects
	def domainObjects = {
		render(contentType: 'application/xml') {
			domainClasses(){
				grailsApplication.domainClasses.each(){
					domainClass( name : it.name )
				}
			}
		}
	}
	
	// retrieves a domain object definition
	def define = {
		def cls
	
		// tried using evaluate but couldn't get it to work
		grailsApplication.domainClasses.each(){
			if( it.name == params.id ){
				cls = it
			}
		}
		
		if( cls ){
			// this is copied from the Grails templates code for 1.0.4 
			// would need to add new ones for plugins like HibernateEvents
			// and add to this list for Grails 1.1.
		    def excludedProps = [
							    'id',
								'version',
							   	Events.ONLOAD_EVENT,
                               	Events.BEFORE_DELETE_EVENT,
                               	Events.BEFORE_INSERT_EVENT,
                               	Events.BEFORE_UPDATE_EVENT
						 	  ]
						
			def props = cls.properties
			
			render(contentType: 'application/xml') {
				model(name:cls.name){
					properties(){
						props.each(){
							
							if(!excludedProps.contains(it.name)){
								def cp = cls.constrainedProperties[it.name]
							
								def assType 
								
								if(it.isManyToMany()) {
								    assType = "many-to-many";
								}
							    else if(it.isOneToMany()) {
									assType = "one-to-many";
								}
								else if(it.isOneToOne()) {
									assType = "one-to-one";
								}
								else if(it.isManyToOne()) {
								    assType = "many-to-one";
								}
								else if(it.isEmbedded()) {
									assType = "embedded";
								}
								
								def inList = cp.getInList()
								
								// the check for String seems to be broken at this point, so move the code from the generation here... 
								def matches = null
								if( cp.hasAppliedConstraint( "matches" )){
									MatchesConstraint c = (MatchesConstraint)this.appliedConstraints.get( "matches" );
								    if(c == null)
								    	matches = null;
									else 
 										matches = c.getRegex();
								}
								
								property(
									name: it.name,
									type: it.type,
									persistent: it.persistent,
									optional: it.optional,
									association: it.association,
									bidirectional: it.bidirectional,
									associationType: assType,
									otherSide : it.otherSide?.name,
									referencedDomainClass : it.referencedDomainClass?.name,
									order: cp.getOrder(),
									format: cp.getFormat(),
									inList: inList?.size() > 0,
									max: cp.getMax(),
									size: cp.getSize(),
									min: cp.getMin(),
									minSize: cp.getMinSize(),
									range: cp.getRange()?.size() > 0,
									scale: cp.getScale(),
									size: cp.getSize(),
									widget: cp.getWidget(),
									display: cp.isDisplay(),
									editable: cp.isEditable(),
									nullable: cp.isNullable(),
									password: cp.isPassword(),
									creditCard : cp.hasAppliedConstraint( "creditCard" ),
									email : cp.hasAppliedConstraint( "email" ),
									blank : cp.hasAppliedConstraint( "blank" ),
									url : cp.hasAppliedConstraint( "url" ),
									matches : matches
								){
									if( inList?.size() > 0){
										inListValues(){
											inList.each(){
												inListValue( value: it )
											}
										}
									}
									if( cp.getRange()?.size() > 0 ){
										range(){
											cp.getRange().each(){
												rangeValue( value: it )
											}
										}
									}
								}
							}
						}
					}
				}
			}
			
		} else {
		
			render(contentType: 'application/xml'){
				error(message:"Domain Class Not Found"){}
			}
		
		}
	
	}
}
